var defaultParams; //默认选中行
var tableData; //定义列表名称
// 删除
function deleteTr() {
if (defaultParams) {
        var ids = [defaultParams.channelId]
  
        layer.confirm('确定删除选中？', {
              title:"温馨提示",
            btn: ['确定', '取消'] //按钮
        }, function() {
            defaultParams = ""
            $.ajax({
                type: "post",
                url: "/operation/channel/delete",
                dataType: "json",
                contentType: 'application/json',
                data: JSON.stringify({ "ids": ids }),
                cache: false,
                success: function(data) {
                    if (data.message === "成功") {
                        layer.msg('删除成功', { time: 1000, icon: 1 });
                        $("#table").bootstrapTable('refresh');
                    } else {
                        layer.msg('删除失败', { time: 1000, icon: 1 });

                    }

                },
                error: function() {
                    layer.msg('请求出错');
                }
            })
        }, function() {
            layer.msg('删除操作取消', {
                time: 2000
            });
              defaultParams = ""
             $("#table").bootstrapTable('refresh');
        });
    } else {
        layer.msg('请选择删除项');
    }

}
//返回
function back() {
    $("#buttonBack").hide()
    $("#buttonAdd").show()
    $("#buttonModify").show()
    $("#buttonDelete").show()
    $(".buttonSearchBox").show()
    $(".bootstrap-table").show()
    $(".contentInput").hide()
    $("#table").bootstrapTable('refresh');
    defaultParams = ""
    $("#submitButton").attr("disabled", false)
}
//修改
function modify() {
    if (defaultParams) {
        // 获取详情
        $.ajax({
            url: "/operation/channel/" + defaultParams["channelId"],
            type: "get",
            dataType: "json",
            cache: false,
            success: function(data) {
                if (data.message === "成功") {
                    $("#systermToute").val(data.data.channelName)
                    $("#moreDesc").val(data.data.remark)
                    $("#type").val(data.data.type)
                }

            },
            error: function(e) {

                if (e.status == 500) {
                    layer.msg("系统错误");
                    return
                } else if (e.responseText) {
                    var errorMessage = JSON.parse(e.responseText).errors[0].message;
                    layer.msg(errorMessage);
                }
            }
        })
        $("#buttonBack").show()
        $("#buttonAdd").hide()
        $("#buttonModify").hide()
        $("#buttonDelete").hide()
        $(".buttonSearchBox").hide()
        $(".bootstrap-table").hide()
        $(".contentInput").show()
        $(".contentInput input").attr("disabled", false)
        $(".contentInput select").attr("disabled", false)
        $(".contentInput textarea").attr("disabled", false)
        $("#submitButton").show()
        $("#saveButton").show()
    } else {
        layer.msg('请选择一项修改');
    }

}
//增加
function add() {
    defaultParams = ""
    $("#subbmitMan").val("loginName")
    $("#organiza").val("depart")
    $("#buttonBack").show()
    $("#buttonAdd").hide()
    $("#buttonModify").hide()
    $("#buttonDelete").hide()
    $(".buttonSearchBox").hide()
    $(".bootstrap-table").hide()
    $(".contentInput").show()
    $(".contentInput input").attr("disabled", false)
    $(".contentInput select").attr("disabled", false)
    $(".contentInput textarea").attr("disabled", false)
    $("#submitButton").show()
    $("#saveButton").show()
    $("#systermToute").val("")
    $("#moreDesc").val("")
    $("#type").val("1")
}

//enter搜索
function noNumbers(e) {
    var keynum;
    var keychar;

    keynum = window.event ? e.keyCode : e.which;
    if (keynum == "13") {
        search()
    }
}
// 搜索
function search() {

    defaultParams = ""
    $("#table").bootstrapTable('destroy');
    tableData.initData()
}
//删除搜索关键字
function removeSearchVal() {
    $("#searchVal").val("")
    $("#searchVal").focus()
}

//提交
function submitButtonEvent() {
    var params = {
        "channelName": $("#systermToute").val(),
        "remark": $("#moreDesc").val(),
        "type": $("#type").val()
    }

    $table = $('#table')
    if (defaultParams.channelId) { //修改

        $.ajax({
            type: "POST",
            contentType: 'application/json',
            url: "/operation/channel/" + defaultParams.channelId,
            data: JSON.stringify(params),
            dataType: "json",
            cache: false,
            success: function(data) {
                layer.msg('修改成功', { time: 1000, icon: 1 });
                $("#submitButton").attr("disabled", true)
                $("#table").bootstrapTable('refresh');
                back()
            },
            error: function(e) {
                var errorMessage = JSON.parse(e.responseText).errors[0].message;
                layer.msg(errorMessage);
            }
        })
    } else { //新增
        $.ajax({
                type: "post",
                url: "/operation/channel/",
                contentType: 'application/json',
                data: JSON.stringify(params),
                dataType: "json",
                cache: false,
                success: function(data) {
                    layer.msg('提交成功', { time: 1000, icon: 1 });
                    $("#submitButton").attr("disabled", true)
                    $("#table").bootstrapTable('refresh');
                    back()
                },
                error: function(e) {
                    if (e.responseText) {
                        var errorMessage = JSON.parse(e.responseText).errors[0].message;
                        layer.msg(errorMessage);
                    }

                }
            })
            //
    }
    return false
}